import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import team.first.domain.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: PACKAGE_NAME
 * @Date: 2021/12/20 13:38
 * @Description:
 */
public class Main {

    private static final String FIELD_TAG = "###";
    private static final String SPLIT_TAG_ONE = "-+-";
    private static final String SPLIT_TAG_TWO = "=|=";
    private static final String LINE_END_TAG = "$$$";

    public static void main(String[] args) {
        // 利用hutool文件工具类读取文件
        FileReader fileReader = new FileReader("movie_data_v3.txt");
        List<String> stringList = fileReader.readLines();
        StringBuffer sb = new StringBuffer();

        for (String s : stringList) {
            // 判断是否一条数据已读完
            if (!s.endsWith(LINE_END_TAG)) {
                sb.append(s);
                continue;
            }

            Movie movie = new Movie();

            // 当前字符串s是包含 $$$ 的一行
            sb.append(StrSplitter.split(s, LINE_END_TAG, 0, true, false).get(0));

            // 读取完一个完整的数据
            String movieString = String.valueOf(sb);

            // 第一次拆分，根据###
            List<String> tuple = StrSplitter.split(movieString, FIELD_TAG, 0, true, false);

            // 第一个是电影编号
            //String movieId = tuple[0];
            movie.setId(Convert.toLong(tuple.get(0)));

            // 第二个是电影片名
            movie.setOriginalTitle(tuple.get(1));

            // 第三个是电影中文名
            movie.setChineseTitle(tuple.get(2));

            // 第四个是电影别名
            movie.setAka(tuple.get(3));

            // 第五个是上映年份
            movie.setYear(Convert.toLong(tuple.get(4)));

            // 第六个是地区
            //String location = tuple[5];
            movie.setRegion(tuple.get(5));

            // 第七个是电影片长
            movie.setDuration(tuple.get(6));

            // 第八个是语言
            movie.setLanguage(tuple.get(7));

            // 第九个是上映时间
            movie.setPubDate(tuple.get(8));

            // 第十个是电影简介
            movie.setSummary(tuple.get(9));

            // 第11个是海报地址
            movie.setPosterUrl(tuple.get(10));
            movie.setPoster(tuple.get(0) + ".jpg");

            // 第12个是分值
            movie.setRating(Convert.toDouble(tuple.get(11)));

            // 影片类型列表(类型编号-+-类型名称)###
            // 第13个是影片类型列表(类型编号:类型名称)
            String movieGenre = tuple.get(12);

            // 对影片类型列表进行分割
            // 第一次分解出每个类型
            List<String> movieGenreList = StrSplitter.split(movieGenre, SPLIT_TAG_TWO, 0, true, false);
            for (String t : movieGenreList) {
                if (t.length() <= 1)
                    continue;
                // 对每个类型在进行解析
                List<String> ts = StrSplitter.split(t, SPLIT_TAG_ONE, 0, true, false);
                Genre genre = new Genre(Convert.toLong(ts.get(0)), ts.get(1));
                try {
                    insertGenre(genre);
                } catch (Exception e) {

                }
                try {
                    insertMovieGenre(movie, genre);
                } catch (SQLException e) {

                }
            }

            // 导演列表(编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址=|=编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址)###
            // 第14个是导演列表(编号:中文名:英文名:性别:地区:生日:简介:照片地址)
            String directorList = tuple.get(13);
            if (directorList.length() != 0) {
                // 解析出每一个导演
                List<String> directorListSplit = StrSplitter.split(directorList, SPLIT_TAG_TWO, 0, true, false);

                for (String d : directorListSplit) {
                    if (d.length() <= 1)
                        continue;
                    // 对每个导演进行解析
                    List<String> ds = StrSplitter.split(d, SPLIT_TAG_ONE, 0, true, false);

                    // 导演没有片中角色名 // Convert.toLong(ds.get(0)), ds.get(1), ds.get(2), ds.get(3), ds.get(4), ds.get(5), ds.get(6), ds.get(7)
                    Staff director = new Staff().setId(Convert.toLong(ds.get(0))).setChineseName(ds.get(1)).setEnglishName(ds.get(2))
                            .setGender(ds.get(3)).setRegion(ds.get(4)).setBirthday(ds.get(5)).setSummary(ds.get(6)).setAvatarUrl(ds.get(7))
                            .setAvatar(ds.get(0) + ".jpg"); // 照片文件名为id+.jpg
                    try{
                        insertStaff(director);
                    } catch (Exception e) {

                    }

                    try {
                        insertMovieCast(movie, director, 2, "导演");
                    } catch (SQLException e) {

                    }
                }
            }

            // 第15个是演员列表(编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址-+-片中角色名=|=
            // (编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址-+-片中角色名)$$$
            String staffList = tuple.get(14);

            // 解析出每一个演员
            List<String> split = StrSplitter.split(staffList, SPLIT_TAG_TWO, 0, true, false);
            // 演员列表(编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址-+-片中角色名=|=
            // (编号-+-中文名-+-英文名-+-性别-+-地区-+-生日-+-简介-+-照片地址-+-片中角色名)$$$
            for (String a : split) {
                if (a.length() <= 1)
                    continue;
                List<String> as = StrSplitter.split(a, SPLIT_TAG_ONE, 0, true, false);

                // 对每个演员进行解析
                Staff staff;
                String actAs = "";
                staff = new Staff().setId(Convert.toLong(as.get(0))).setChineseName(as.get(1)).setEnglishName(as.get(2))
                        .setGender(as.get(3)).setRegion(as.get(4)).setBirthday(as.get(5)).setSummary(as.get(6))
                        .setAvatarUrl(as.get(7)).setAvatar(as.get(0) + ".jpg");
                if (as.size() == 9) {
                    // 7340条
                    actAs = as.get(8);
                }

                try {
                    insertStaff(staff);
                } catch (Exception e) {

                }

                try {
                    insertMovieCast(movie, staff, 1, actAs);
                } catch (SQLException e) {

                }
            }

            sb = new StringBuffer();
            try {
                insertMovie(movie);
            } catch (SQLException e) {

            }
        }
    }

    static void insertMovieGenre(Movie movie, Genre genre) throws SQLException {
        Db.use().insert(
                Entity.create("public.movie_genre")
                        .set("movie_id", movie.getId())
                        .set("genre_id", genre.getId())
        );
    }

    static void insertMovieCast(Movie movie, Staff staff, Integer identity, String actAs) throws SQLException {
        Db.use().insert(
                Entity.create("public.movie_cast")
                        .set("movie_id", movie.getId())
                        .set("staff_id", staff.getId())
                        .set("role_type_id", identity)
                        .set("act_as", actAs)
        );
    }

    static void insertMovie(Movie movie) throws SQLException {
        Db.use().insert(
                Entity.create("public.movie")
                        .set("id", movie.getId())
                        .set("original_title", movie.getOriginalTitle())
                        .set("chinese_title", movie.getChineseTitle())
                        .set("aka", movie.getAka())
                        .set("year", movie.getYear())
                        .set("region", movie.getRegion())
                        .set("duration", movie.getDuration())
                        .set("language", movie.getLanguage())
                        .set("pub_date", movie.getPubDate())
                        .set("summary", movie.getSummary())
                        .set("poster_url", movie.getPosterUrl())
                        .set("rating", movie.getRating())
                        .set("poster", movie.getPoster())
        );
    }

    static void insertGenre(Genre genre) throws SQLException {
        Db.use().insert(
                Entity.create("public.genre")
                        .set("id", genre.getId())
                        .set("name", genre.getName())
        );
    }

    static void insertStaff(Staff staff) throws SQLException {
        Db.use().insert(
                Entity.create("public.staff")
                        .set("id", staff.getId())
                        .set("chinese_name", staff.getChineseName())
                        .set("english_name", staff.getEnglishName())
                        .set("gender", staff.getGender())
                        .set("region", staff.getRegion())
                        .set("birthday", staff.getBirthday())
                        .set("summary", staff.getSummary())
                        .set("avatar_url", staff.getAvatarUrl())
                        .set("avatar", staff.getAvatar())
        );
    }
}
